<?php

	header("Content-Type: text/html;charset=utf-8");

	include("function/_nosql.php"); //

	include("function/function.php");

	

	$action = chkstr(trim($_REQUEST['action']));

	$user_email = chkstr(trim($_REQUEST['user_email']));

	$user_emailcode = chkstr(trim($_REQUEST['user_emailcode']));

	$user_password = chkstr(trim($_REQUEST['user_password']));

	$user_repassword = chkstr(trim($_REQUEST['user_repassword']));

	$redirect_urls = chkstr(trim($_REQUEST['redirect_urls']));

	$user_qq_openid = $_SESSION['kboy_qq_login'];

	$user_weibo_openid = $_SESSION['kboy_weibo_login'];

	$user_phone = chkstr(trim($_REQUEST['user_phone']));

	$user_realname = chkstr(trim($_REQUEST['user_realname']));

	$user_phonecode = chkstr(trim($_REQUEST['user_phonecode']));

	$user_face = "/images/lvyou.jpg";

	

//	$_SESSION['EmailCode'] = 1234;

//	$_SESSION['EmailName'] = $user_email;

	

	

	if($action == "chkuser"){

		if(!isEmail($user_email)){

			echo "邮箱格式错误！"; // 用户名格式不正确

			exit();

		}

		$is_reguser = getNewsNums("kboy_user","user_email='$user_email'");

		if($is_reguser >0){

			echo "该邮箱已被注册！";

			exit();

		}else{

			echo "1";

			exit();

		}

	}

	else if($action == 'chkuserphone' ){//验证手机号  

	    if(!isMobile($user_phone)){

			echo "手机号格式错误！"; // 用户名格式不正确

			exit();

		}

		$is_reguser = getNewsNums("kboy_user","user_phone='$user_phone'");

		if($is_reguser >0){

			echo "该手机号已被注册！";

			exit();

		}else{

			echo "1";

			exit();

		}

	}

	elseif($action == "save"){

		// 验证码验证

		if(strlen($user_emailcode)<1){

			echo erro("请正确输入验证码1！");

		}

		if($user_email != $_SESSION['EmailName']){

			echo erro("请正确输入验证码2！");

		}

		if($user_emailcode != $_SESSION['EmailCode']){

			echo erro("请正确输入验证码3！");

		}



		// 邮箱验证

		if(!isEmail($user_email)){

			echo erro("邮箱格式错误！");

		}



		// 密码验证

		if(strlen($user_password)<5){

			echo erro("密码不能少于5位字符！");

		}

		if(strlen($user_repassword)<5){

			echo erro("两次密码输入不一致！");

		}

		if($user_password != $user_repassword){

			echo erro("两次密码输入不一致！");

		}

		if(strlen($user_realname)<2){

			echo erro("请输入真实姓名！");

		}

		

		

		$is_reguser = getNewsNums("kboy_user","user_email='$user_email'");

		if($is_reguser >0){

			echo erro("该邮箱已被注册！");

		}

		

		if(getNewsNums("kboy_user","user_qq_openid='$user_qq_openid'") >0){

			$_SESSION['kboy_qq_login'] = "";

		}

		

		

		

		$user_password = md5(md5($user_password)); // 双层MD5加密

		// 添加数据

		$mysql = new sqlstr("kboy_user"); 

		$mysql -> set("user_email",$user_email);

		if($_SESSION['kboy_qq_login']){

			$mysql -> set("user_qq_openid",$user_qq_openid);

		}

		if($_SESSION['kboy_weibo_login']){

			$mysql -> set("user_weibo_openid",$user_weibo_openid);

		}

		

		$mysql -> set("user_realname",$user_realname);

		$mysql -> set("user_password",$user_password);

		$mysql -> set("user_face",$user_face);

	

		$news_sql = $mysql -> insertSql();

		$news_res = mysql_query($news_sql);

		if($news_res){

		

			$_SESSION['kboy_user'] = $user_email;

			$_SESSION['kboy_userid'] = getClassName("kboy_user","uid","user_email='$user_email'");

			$_SESSION['EmailName'] = "";

			$_SESSION['EmailCode'] = "";

			$_SESSION['kboy_qq_login'] = "";

			$_SESSION['kboy_weibo_login'] = "";



			mysql_query("update kboy_user set user_lasttime='$THIS_TODAY_DATETIME',user_lastip='".getip()."' where user_email='$user_email'");



			if($redirect_urls){

				echo ok("恭喜您注册成功",urldecode($redirect_urls));

			}else{

				echo ok("恭喜您注册成功","user_index.php");

			}

		}else{

			echo erro("服务器内部错误");

		}

		

	}

	elseif($action == "savephone"){//手机注册

	   	// 手机号验证

		if(!isMobile($user_phone)){

			echo erro("手机号格式错误！");

		}

		if($user_phone != $_SESSION['PhoneName']){

			echo erro("手机号有误！");

		}

		if($user_phonecode != $_SESSION['PhoneCode']){

			echo erro("请正确输入短信验证码！");

		}



		// 密码验证

		if(strlen($user_password)<5){

			echo erro("密码不能少于5位字符！");

		}

		if(strlen($user_repassword)<5){

			echo erro("两次密码输入不一致！");

		}

		if($user_password != $user_repassword){

			echo erro("两次密码输入不一致！");

		}

		

		$is_reguser = getNewsNums("kboy_user","user_phone='$user_phone'");

		if($is_reguser >0){

			echo erro("该手机号已被注册！");

		}

		

		if(getNewsNums("kboy_user","user_qq_openid='$user_qq_openid'") >0){

			$_SESSION['kboy_qq_login'] = "";

		}

		

		

		

		$user_password = md5(md5($user_password)); // 双层MD5加密

		// 添加数据

		$mysql = new sqlstr("kboy_user"); 

		$mysql -> set("user_phone",$user_phone);
		
		if($_SESSION['kboy_qq_login']){

			$mysql -> set("user_qq_openid",$user_qq_openid);

		}

		if($_SESSION['kboy_weibo_login']){

			$mysql -> set("user_weibo_openid",$user_weibo_openid);

		}

		

		$mysql -> set("user_password",$user_password);
		$mysql -> set("user_face",$user_face);
		$news_sql = $mysql -> insertSql();

		$news_res = mysql_query($news_sql);

		if($news_res){

		

			$_SESSION['kboy_user'] = $user_phone;

			$_SESSION['kboy_userid'] = getClassName("kboy_user","uid","user_phone='$user_phone'");

			$_SESSION['PhoneName'] = "";

			$_SESSION['PhoneCode'] = "";

			$_SESSION['kboy_qq_login'] = "";

			$_SESSION['kboy_weibo_login'] = "";



			mysql_query("update kboy_user set user_lasttime='$THIS_TODAY_DATETIME',user_lastip='".getip()."' where user_phone='$user_phone'");



//			if($redirect_urls){

//				echo ok("恭喜您注册成功",urldecode($redirect_urls));

//			}else{

				echo ok("恭喜您注册成功","user_index.php");

//			}

		}else{

			echo erro("服务器内部错误");

		}

	}

	else{

		echo erro("参数错误");

	}

	

?>